const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    helpList: [],
    falseList: [],
    // 领取按钮
    isBtn: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHelpList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let name = "什么？免费领一盒凤梨酥！快点！";
    let image = "http://qiniu.hiima.cn/8636ee590090b1f7b1db1f82ed71768a82e9a8cd1603448634";
    let userID = wx.getStorageSync('userID');
    return {
      title: name,
      path: '/pages/index/index?uid=' + userID,
      imageUrl: image
    }
  },
  // 获取助力列表
  getHelpList() {
    let that = this;
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.getHelp, {
      userID: userID,
      status: 1
    }).then(function (res) {
      if (res.code === 200) {
        var arr = [];
        if (res.content.count < 3) {
          var num = 3 - res.content.count;
          for (var i = 0; i < num; i++) {
            arr.push("待助力");
          }
        }
        that.setData({
          isBtn: res.content.count >= 3 ? 1 : 0,
          helpList: res.content.data,
          falseList: arr
        });
      }
    });
  },
  toReceive() {
    let that = this
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.toReceive, {
      userID: userID,
    }).then(function (res) {
      if (res.code == 200) {
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 1000
        })
        that.getHelpList();
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
      }
    });
  }
})